

// 获取全局应用程序实例对象
const app = getApp();

// 创建页面实例对象
Page({
  /**
   * 页面名称
   */
  name: "index",
  /**
   * 页面的初始数据
   */

  data: {
    menu: [],
    jrcolor: "255,255,255",
    zrcolor: "200,200,200",
    bs: "0000",
    ztrank: "未能获取",
    mynick:"",
    myrank:"",
    mytouxiang:"",
    mydanwei:"",
    myname:"",
    mybs:"",
    hasbd:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    var this1 = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          console.log(res.code )
          var rescode=res.code;
          console.log(rescode)
          wx.request({
            url: 'https://deepspread.com/gonghui/login.php?appid=wx7e0074cbe02b7c91&appsecreat=f24ca35f4faa5fc914805c2061ee45a1&jscode=' + rescode, //仅为示例，并非真实的接口地址
            data: {

            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success: function (res) {
              console.log(res.data)
              var jsonStr = res.data;
              jsonStr = jsonStr.replace(" ", "");
              if (typeof jsonStr != 'object') {
                jsonStr = jsonStr.replace(/\ufeff/g, "");//重点
                var jj = JSON.parse(jsonStr);

              }
              app.sessionkey = jj.session_key;
              app.openid = jj.openid;
              wx.getUserInfo({
                success: function (res) {
                  var userInfo = res.userInfo
                  var nickName = userInfo.nickName
                  var avatarUrl = userInfo.avatarUrl
                  var gender = userInfo.gender //性别 0：未知、1：男、2：女
                  var province = userInfo.province
                  var city = userInfo.city
                  var country = userInfo.country
                  app.nickname = nickName;
                  app.touxiang = avatarUrl;
                  app.gender = gender;
                  app.city = city;
                  app.province = province;
                  this1.setData({ mytouxiang: app.touxiang})
                }
              });
              wx.getWeRunData({
                success(res) {
                  const encryptedData = res.encryptedData;
                  var iv1 = res.iv;
                  console.log(res);

                  wx.request({
                    url: 'https://deepspread.com/gonghui/getsteps.php', //仅为示例，并非真实的接口地址
                    data: {
                      key: app.sessionkey,
                      iv: iv1,
                      data: encryptedData,
                    },
                    header: {
                      'content-type': 'application/json' // 默认值
                    },
                    success: function (res) {
                      var jsonStr = res.data;
                      jsonStr = jsonStr.replace(" ", "");
                      if (typeof jsonStr != 'object') {
                        jsonStr = jsonStr.replace(/\ufeff/g, "");//重点
                        var jj = JSON.parse(jsonStr);
                        console.log(jj);
                        app.jtbs = jj.stepInfoList[30].step;
                        app.ztbs = jj.stepInfoList[29].step;
                        console.log(app.jtbs + " " + app.ztbs);
                 
                        this1.setData({ bs: app.jtbs, mybs:app.jtbs});
                       
                      }
                    }
                  })

                }
              });
              wx.request({
                url: 'https://deepspread.com/gonghui/getmyrank.php', //仅为示例，并非真实的接口地址
                data: {
                  openid: app.openid
                },
                header: {
                  'content-type': 'application/json' // 默认值
                },
                success: function (res) {
                  console.log("getrank:"+res.data)
                  console.log(res.data)
                  var name=res.data.name+""
                  if(name.length<2)
                  {
                    res.data.name ="请先注册绑定";
                  }
                  this1.setData({ ztrank: res.data.rank, myrank: res.data.rank,myname:res.data.name,mydanwei:res.data.danwei });
                }
              })

            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });
    wx.request({
      url: 'https://deepspread.com/gonghui/getjtbs.php', //仅为示例，并非真实的接口地址
      data: {
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)
        this1.setData({ menu: res.data });
      }
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.judgezhuce();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },
  bangding() {
    wx.request({
      url: 'https://deepspread.com/gonghui/judgeuser.php', //仅为示例，并非真实的接口地址
      data: {
        openid: app.openid,

      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)
        var rs = res.data + "";
        if (rs.indexOf("exsit") > -1) {
          wx.showToast({
            title: '已绑定',
            icon: 'none',
            duration: 2000
          })
        }
        else {
          wx.navigateTo({
            url: '/page/page1/page1' //test是新打开的页面，问号后面的id是传的参数，到test页面用options接收。
          })
        }
      }
    })
    
  },
  judgezhuce()
  {
    var this1=this;
   
    wx.request({
      url: 'https://deepspread.com/gonghui/judgeuser.php', //仅为示例，并非真实的接口地址
      data: {
        openid: app.openid,

      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)
        var s=res.data+"";
        console.log("s:"+s.length)
        if(s.length>1)
        {
          this1.data.hasbd=true;
         
        }
      }
    })

  },
  shangchuan() {
    var this1=this;
    wx.request({
      url: 'https://deepspread.com/gonghui/judgeuser.php', //仅为示例，并非真实的接口地址
      data: {
        openid: app.openid,

      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)
        var rs = res.data + "";
        if (rs.indexOf("exsit") > -1) {
          wx.request({
            url: 'https://deepspread.com/gonghui/updatebs.php', //仅为示例，并非真实的接口地址
            data: {
              openid: app.openid,
              ztbs: app.ztbs,
              jtbs: app.jtbs,
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success: function (res) {
              console.log("res:"+res.data)
              var ress=res.data+"";
              if (ress.indexOf("newupdate")>-1)
              {
                wx.showToast({
                  title: '成功 积分+1',
                  icon: 'none',
                  duration: 2000
                })
                this1.jr();
              }
              else if (ress.indexOf("hadupdate")>-1)
              {
                wx.showToast({
                  title: '成功 积分+0',
                  icon: 'none',
                  duration: 2000
                })
                this1.jr();
              }
              wx.request({
                url: 'https://deepspread.com/gonghui/getmyrank.php', //仅为示例，并非真实的接口地址
                data: {
                  openid: app.openid
                },
                header: {
                  'content-type': 'application/json' // 默认值
                },
                success: function (res) {
                  console.log("getrank:" + res.data)
                  console.log(res.data)
                  var name = res.data.name + ""
                  if (name.length < 2) {
                    res.data.name = "请先注册绑定";
                  }
                  this1.setData({ ztrank: res.data.rank, myrank: res.data.rank, myname: res.data.name, mydanwei: res.data.danwei });
                }
              })
            }
          })
        }
        else {
          wx.showToast({
            title: '未绑定',
            icon: 'none',
            duration: 2000
          })
        }
      }
    })


  },
  choujiang() {

    if (this.data.hasbd == true) {
      wx.navigateTo({
        url: '/page/page3/page3'
      })
    }
    else {
      wx.showToast({
        title: '未绑定',
        icon: 'none',
        duration: 2000
      })
    }
  },
  jr() {
    var this1 = this;
    this.setData({ jrcolor: "255,255,255", zrcolor: "230,230,230" })
    wx.request({
      url: 'https://deepspread.com/gonghui/getjtbs.php', //仅为示例，并非真实的接口地址
      data: {
        openid:app.openid,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)
        this1.setData({ menu: res.data });
      }
    })
  },
  zr() {
    var this1 = this;
    this.setData({ jrcolor: "230,230,230", zrcolor: "255,255,255" })
    wx.request({
      url: 'https://deepspread.com/gonghui/getztbs.php', //仅为示例，并非真实的接口地址
      data: {
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)
        this1.setData({ menu: res.data });
      }
    })
  },
  onShareAppMessage: function () {
    return {
      title: '交大工会',
      desc: '交大工会小程序',
      path: '/page/index/index'
    }
  },
  duihuan()
  {
    if (this.data.hasbd==true)
    {
      wx.navigateTo({
        url: '/page/page4/page4'
      })
    }
    else
    {
      wx.showToast({
        title: '未绑定',
        icon: 'none',
        duration: 2000
      })
    }
  }
  //以下为自定义点击事件

})

//UPDATE `userinfo` SET `jtbs`='0',`todayhasupdate`='0'